
<?php
/**
 * 用户中心-个人信息
 * User: tengyu
 * Date: 2017/8/25
 * Time: 11:48
 */?>
@extends('common.common')
@section('title','用户-个人中心')
@section('css')<link rel="stylesheet" href="{{asset('/css/personal_information.css')}}">@endsection
@section('js')<script src="{{asset('/js/personal_information.js')}}"></script>@endsection
@section('content')
    <div id="content">
        @include('common.centernav')
        @if (count($errors) > 0)
            <div class="alert alert-danger text-left">
                <strong>注册信息不符合规范!</strong>
                <ul>
                    @if(is_object($errors))
                        @foreach ($errors->all() as $error)
                            <li>{{ $error }}</li>
                        @endforeach
                    @else
                        <li>{{$errors}}</li>
                    @endif
                </ul>
            </div>
        @endif
        <h3>个人中心</h3>
        <div class="container">
            <div class="row">
                <!--sidebar start-->
                @include('usercenter.usersidebar')
                <!--sidebar end-->
                <!--个人信息修改 start-->
                <div class="col-xs-9">
                    <!--右侧面版-->
                    <div class="panel panel-default rightPanel">
                        <div class="panel-heading"><h4>个人信息</h4></div>
                        <div class="panel-body">
                            <!--2、列表组-->
                            <ul class="list-group">
                                <li class="list-group-item">基本资料</li>
                                <li class="list-group-item">
                                    <!--5、媒体-->
                                    <div id="comments">
                                        <div class="media">
                                            <div class="media-left">
                                                @if(!$user->avator)
                                                <img src="{{asset('/img/181509094162_.pic_hd.jpg')}}" style="width:143px;height:143px;">
                                                    @else
                                                    <img src="{{asset("$user->avator")}}" style="width: 143px;height: 143px;">
                                                @endif
                                            </div>
                                            <div class="media-body">
                                                <p>用户名&nbsp;:&nbsp;{{$user->name}}</p>
                                                <p>注册时间&nbsp;:&nbsp;{{$user->created_at}}</p>
                                                <p>绑定邮箱&nbsp;:&nbsp;<span>{{$user->email}}</span></p>
                                            </div>
                                        </div>
                                    </div>
                                    <button id="changePic" class="btn btn-warning"  data-toggle="modal" data-target="#imgModalBox">修改头像</button>
                                </li>
                            </ul>
                        </div>
                        <!--change start-->
                        <div class="changeMyInfo">
                            <p>
                                <img src="{{asset('/img/yellowchoose.png')}}" alt="">
                                已设置
                            </p>
                            <span>账户昵称</span>
                            <span class="nowName">当前账户昵称&nbsp;:&nbsp;{{$user->name}}</span>
                            <i data-toggle="modal" data-target="#nameModalBox">修改</i>
                        </div>
                        <div class="changeMyInfo">
                            <p>
                                @if(!$user->email)
                                    <img src="{{asset('/img/choose.png')}}" alt="">
                                    未设置
                                    @else
                                    <img src="{{asset('/img/yellowchoose.png')}}" alt="">
                                    已设置
                                @endif
                            </p>
                            <span>电子邮箱</span>
                            <span class="nowName">当前邮箱&nbsp;:&nbsp;{{$user->email}}</span>
                            <i data-toggle="modal" data-target="#emailModalBox">修改</i>
                        </div>
                        <div class="changeMyInfo">
                            <p>
                                <img src="{{asset('/img/yellowchoose.png')}}" alt="">
                                已设置
                            </p>
                            <span>登录密码</span>
                            <span class="nowName">用于登录芈租界时需要的密码</span>
                            <i data-toggle="modal" data-target="#pwdModalBox">修改</i>
                        </div>
                        <!--change end-->
                    </div>
                </div>
                <!--个人信息修改 end-->
            </div>
        </div>
    </div>
    <!--修改密码模态框-->
    <div id="pwdModalBox" class="modal" data-backdrop="static">
        <div class="modal-dialog modal-sm">
            <div class="modal-content">
                <div class="modal-header">
                    <span class="close" data-dismiss="modal">&times;</span>
                    <h4 class="modal-title">修改密码</h4>
                </div>
                <div class="modal-body">
                    <form class="form-inline">
                        <div class="form-group">
                            <label class="control-label">原密码:</label>
                            <input type="password" name="oldpassword" value="" class="form-control" style="width:202px;">
                        </div>
                        <div class="form-group">
                            <label class="control-label">新密码:</label>
                            <input type="password" name="password" value="" class="form-control" style="width:202px;">
                        </div>
                        <div class="form-group">
                            <label class="control-label">重复密码:</label>
                            <input type="password" name="repassword" value="" class="form-control" style="width:202px;">
                        </div>
                    </form>
                </div>
                <div class="modal-footer">
                    <button type="button" class="btn btn-warning" data-dismiss="modal" onclick="userpassword_edit('{{$user->id}}')">确认重置</button>
                </div>
            </div>
        </div>
    </div>
    <!--修改邮箱模态框-->
    <div id="emailModalBox" class="modal" data-backdrop="static">
        <div class="modal-dialog modal-sm">
            <div class="modal-content">
                <div class="modal-header">
                    <span class="close" data-dismiss="modal">&times;</span>
                    <h4 class="modal-title">修改邮箱</h4>
                </div>
                <div class="modal-body">
                    <form class="form-inline">
                        <div class="form-group">
                            <label class="control-label">当前邮箱:</label>
                            {{$user->email}}
                        </div>
                        <div class="form-group">
                            <label class="control-label">新邮箱:</label>
                            <input type="email" class="form-control" name="email" value="" style="width:202px;">
                        </div>
                    </form>
                </div>
                <div class="modal-footer">
                    <button type="button" class="btn btn-warning" data-dismiss="modal" onclick="useremail_edit('{{$user->email}}','{{$user->id}}')">确认修改</button>
                </div>
            </div>
        </div>
    </div>
    <!--修改昵称模态框-->
    <div id="nameModalBox" class="modal" data-backdrop="static">
        <div class="modal-dialog modal-sm">
            <div class="modal-content">
                <div class="modal-header">
                    <span class="close" data-dismiss="modal">&times;</span>
                    <h4 class="modal-title">修改昵称</h4>
                </div>
                <div class="modal-body">
                    <form class="form-inline">
                        <div class="form-group">
                            <label class="control-label">当前昵称:</label>
                            {{$user->name}}
                        </div>
                        <div class="form-group">
                            <label class="control-label">新昵称:</label>
                            <input type="text" class="form-control" name="name" value="" style="width:202px;">
                        </div>
                    </form>
                </div>
                <div class="modal-footer">
                    <button type="button" class="btn btn-warning" data-dismiss="modal" onclick="username_edit('{{$user->name}}','{{$user->id}}')">确认修改</button>
                </div>
            </div>
        </div>
    </div>
    <!--修改头像模态框-->
    <div id="imgModalBox" class="modal" data-backdrop="static">
        <div class="modal-dialog">
            <div class="modal-content">
                <div class="modal-header">
                    <span class="close" data-dismiss="modal">&times;</span>
                    <h4 class="modal-title">修改头像</h4>
                </div>
                <div class="modal-body">
                    <form id="useravatar" action="/user/avatar/{{$user->id}}" method="post" enctype="multipart/form-data">
                        {!! csrf_field() !!}
                        <div class="lookpic">
                            @if(!$user->avator)
                                <img src="{{asset('/img/smallpic.jpg')}}" alt="头像" style="width:143px;height:143px;">
                                @else
                                <img src="{{$user->avator}}" alt="头像" style="width:145px;height:145px;">
                            @endif

                            <p>头像预览</p>
                            <div>
                                <input type="file" id="inputs" name="avatar" value="">
                                <a href="JavaScript:void(0)" class="btn btn-warning">上传图片</a>
                            </div>
                        </div>
                        <div class="lookRight" id="upload">

                        </div>
                    </form>
                </div>
                <div class="modal-footer">
                    <button type="button" class="btn btn-warning" data-dismiss="modal" onclick="useravatar_edit()">确认修改</button>
                </div>
            </div>
        </div>
    </div>

    <script>
        /**name修改*/
        function username_edit(oldname,id)
        {
            var name = $("#nameModalBox input[name='name']").val();
            if(oldname !== name && name !=''){
                $.ajax({
                    url:'/user/update/'+id,
                    type:'post',
                    headers: { 'X-CSRF-TOKEN' : '{{ csrf_token() }}' },
                    data:{'name':name,'type':'name'},
                    dataType:'json',
                    success:function(res){
                        if(res.res=='ok'){
                           alert(res.msg);
                            history.go(0);
                        }else{
                            alert(res.msg);
                        }
                    }
                });
            }else{
                alert('用户昵称没有变化!');
            }
        }
        /**email修改*/
        function useremail_edit(oldemail,id)
        {
            var email = $("#emailModalBox input[name='email']").val();
            if(email != oldemail && email !=''){
                $.ajax({
                    url:'/user/update/'+id,
                    type:'post',
                    headers: { 'X-CSRF-TOKEN' : '{{ csrf_token() }}' },
                    data:{'email':email,'type':'email'},
                    dataType:'json',
                    success:function(res) {
                        if (res.res == 'ok') {
                            alert(res.msg);
                            history.go(0);
                        } else {
                            alert(res.msg);
                        }
                    }
                });
            }else{
                alert('绑定邮箱没有变化!');
            }
        }
        /**password修改*/
        function userpassword_edit(id)
        {
            var oldpassword = $("#pwdModalBox input[name='oldpassword']").val();
            var password = $("#pwdModalBox input[name='password']").val();
            var repassword =  $("#pwdModalBox input[name='repassword']").val();
            $.ajax({
                url:'/user/update/'+id,
                type:'post',
                headers: { 'X-CSRF-TOKEN' : '{{ csrf_token() }}' },
                data:{'oldpassword':oldpassword,'password':password,'repassword':repassword,'type':'password'},
                dataType:'json',
                success:function(res){
                    if(res.res=='ok'){
                        alert(res.msg);
                        history.go(0);
                    }else{
                        alert(res.msg);
                    }
                }
            });

        }
        /**修改头像*/
        function useravatar_edit(){
            $("#useravatar").submit();
        }

    </script>
@endsection

